package com.aspose.cells;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class NameCollection extends CollectionBase {
   private WorksheetCollection b;
   private HashMap c;

   NameCollection(WorksheetCollection sheets) {
      this.b = sheets;
   }

   WorksheetCollection a() {
      return this.b;
   }

   Range[] a(WorksheetCollection var1) {
      ArrayList var2 = new ArrayList();

      for(int var3 = 0; var3 < this.a.size(); ++var3) {
         Name var4 = (Name)this.a.get(var3);
         if (var4.n() != 0) {
            Range var5 = var4.s();
            if (var5 != null) {
               com.aspose.cells.b.a.a.ze.a(var2, (Object)var5);
            }
         }
      }

      if (var2.size() == 0) {
         return null;
      } else {
         Range[] var6 = new Range[var2.size()];
         com.aspose.cells.b.a.a.ze.a(var2, (Object[])var6);
         return var6;
      }
   }

   public int add(String text) {
      byte var2 = -1;
      int[] var3 = new int[]{var2};
      text = this.a(text, var3);
      int var4 = var3[0];
      return this.a(var4, text);
   }

   int a(int var1, String var2) {
      int var3 = this.a(var2, var1, false);
      if (var3 != -1) {
         return var3;
      } else {
         Name var4 = new Name(this.b, var2);
         var4.setSheetIndex(var1 + 1);
         return this.a(var4, true);
      }
   }

   int b(int var1, String var2) {
      int var3 = this.a(var2, var1, true);
      if (var3 != -1) {
         return var3;
      } else {
         Name var4 = new Name(this.b, var2);
         var4.setSheetIndex(var1 + 1);
         return this.a(var4, true);
      }
   }

   private HashMap d() {
      if (this.c == null) {
         this.c = new HashMap();

         for(int var1 = 0; var1 < this.getCount(); ++var1) {
            Name var2 = this.get(var1);
            String var3 = e(var2.getSheetIndex() - 1, var2.getText().toUpperCase());
            if (this.c.get(var3) == null) {
               this.c.put(var3, var1);
            }
         }
      }

      return this.c;
   }

   int a(Name var1, boolean var2) {
      int var3 = this.getCount();
      if (var2) {
         String var4 = e(var1.getSheetIndex() - 1, var1.getText());
         this.d().put(var4, var3);
      }

      com.aspose.cells.b.a.a.ze.a(this.a, (Object)var1);
      return var3;
   }

   void a(boolean var1) {
      int var2;
      Name var3;
      if (var1) {
         for(var2 = 0; var2 < this.getCount(); ++var2) {
            var3 = this.get(var2);
            if (var3.b() == null) {
               var3.setRefersTo(var3.getRefersTo());
            }
         }
      } else {
         for(var2 = 0; var2 < this.getCount(); ++var2) {
            var3 = this.get(var2);
            if (var3.b() == null) {
               var3.i(var3.getRefersTo());
            }
         }
      }

   }

   public Name get(int index) {
      return (Name)this.a.get(index);
   }

   public Name get(String text) {
      text = text.toUpperCase();
      byte var2 = -1;
      int[] var3 = new int[]{var2};
      text = this.a(text, var3);
      int var5 = var3[0];
      int var4 = this.a(text, var5, false);
      return var4 == -1 ? null : this.get(var4);
   }

   private void a(HashMap var1) {
      this.c = null;

      int var7;
      for(int var2 = 0; var2 < this.b.getCount(); ++var2) {
         Worksheet var3 = this.b.get(var2);
         zaau var4 = var3.getCells().b;
         if (var4.c > 0) {
            zyi[] var5 = var4.b;

            for(var7 = 0; var7 < var5.length; ++var7) {
               if (var5[var7] != null) {
                  zyi var6 = var5[var7];
                  if (var6.a() != null) {
                     zyo.a(var6.a().d(), -1, -1, (HashMap)var1, (WorksheetCollection)this.b);
                  } else {
                     zyo.a(var6.b, -1, -1, (HashMap)var1, (WorksheetCollection)this.b);
                  }
               }
            }
         }

         if (var3.t() != null && var3.getCharts().getCount() > 0) {
            var3.getCharts().b(var1);
         }

         ConditionalFormattingCollection var16 = var3.getConditionalFormattings();

         for(int var18 = 0; var18 < var16.getCount(); ++var18) {
            FormatConditionCollection var21 = var16.get(var18);
            if (var21.getRangeCount() != 0) {
               for(int var8 = 0; var8 < var21.getCount(); ++var8) {
                  FormatCondition var9 = var21.get(var8);
                  var9.f();
                  byte[] var10;
                  if (var9.a() != null) {
                     var10 = var9.a();
                     zyo.a(var10, -1, var10.length - 1, (HashMap)var1, (WorksheetCollection)this.b);
                  }

                  if (var9.c() != null) {
                     var10 = var9.c();
                     zyo.a(var10, -1, var10.length - 1, (HashMap)var1, (WorksheetCollection)this.b);
                  }
               }
            }
         }

         ValidationCollection var19 = var3.getValidations();

         for(var7 = 0; var7 < var19.getCount(); ++var7) {
            Validation var22 = var19.get(var7);
            byte[] var24;
            if (var22.d() != null) {
               var24 = (byte[])var22.d();
               zyo.a(var24, 0, var24.length - 1, (HashMap)var1, (WorksheetCollection)this.b);
            }

            if (var22.e() != null) {
               var24 = (byte[])var22.e();
               zyo.a(var24, 0, var24.length - 1, (HashMap)var1, (WorksheetCollection)this.b);
            }
         }
      }

      com.aspose.cells.b.a.a.zd var11 = new com.aspose.cells.b.a.a.zd();
      Iterator var12 = var1.keySet().iterator();

      while(var12.hasNext()) {
         Object var14 = var12.next();
         var11.a(var14, var1.get(var14));
      }

      int var13 = 0;
      int var15 = -1;
      HashMap var17 = new HashMap();
      Iterator var20 = var11.c().iterator();

      while(true) {
         while(var20.hasNext()) {
            var7 = (Integer)var20.next();
            if ((Boolean)var11.d((Object)var7)) {
               Name var23 = (Name)this.a.get(var7);
               var23.u();
            } else {
               if (var15 != -1 && var15 + 1 != var7) {
                  ++var15;

                  while(var15 < var7) {
                     var17.put(var15, var15 - var13);
                     ++var15;
                  }
               }

               var15 = var7;
               ++var13;
            }
         }

         if (var15 == -1) {
            return;
         }

         if (var15 < this.getCount()) {
            ++var15;

            while(var15 < this.getCount()) {
               var17.put(var15, var15 - var13);
               ++var15;
            }
         }

         var13 = 0;
         var20 = var11.c().iterator();

         while(var20.hasNext()) {
            var7 = (Integer)var20.next();
            if (!(Boolean)var11.d((Object)var7)) {
               this.a.remove(var7 - var13);
               ++var13;
            }
         }

         this.b(var17);
         return;
      }
   }

   public void remove(String[] names) {
      HashMap var2 = new HashMap();

      for(int var3 = 0; var3 < names.length; ++var3) {
         String var4 = names[var3];
         byte var5 = -1;
         int[] var6 = new int[]{var5};
         var4 = this.a(var4, var6);
         int var8 = var6[0];
         int var7 = this.a(var4, var8, false);
         if (var7 != -1) {
            var2.put(var7, false);
         }
      }

      this.a(var2);
   }

   public void remove(String text) {
      byte var2 = -1;
      int[] var3 = new int[]{var2};
      text = this.a(text, var3);
      int var5 = var3[0];
      int var4 = this.a(text, var5, false);
      if (var4 != -1) {
         this.removeAt(var4);
      }

   }

   public void removeAt(int index) {
      Name var2 = this.get(index);
      if (var2.isReferred()) {
         var2.u();
      } else {
         this.c = null;
         HashMap var3 = new HashMap();

         for(int var4 = index + 1; var4 < this.getCount(); ++var4) {
            var3.put(var4, var4 - 1);
         }

         this.a.remove(index);
         this.b(var3);
      }
   }

   private boolean[] e() {
      HashMap var1 = new HashMap();

      for(int var2 = 0; var2 < this.getCount(); ++var2) {
         var1.put(var2, zml.e);
      }

      Cell var12 = null;

      for(int var3 = 0; var3 < this.b.getCount(); ++var3) {
         Cells var4 = this.b.get(var3).getCells();

         int var7;
         for(int var5 = 0; var5 < var4.getRows().getCount(); ++var5) {
            Row var6 = var4.getRows().getRowByIndex(var5);

            for(var7 = 0; var7 < var6.a(); ++var7) {
               var12 = var6.getCellByIndex(var7);
               if (var12.f()) {
                  if (var12.D() != null) {
                     zyo.a(var12.D().d(), -1, -1, (HashMap)var1, (WorksheetCollection)this.b);
                  } else {
                     zyo.a(var12.v(), -1, -1, (HashMap)var1, (WorksheetCollection)this.b);
                  }
               }
            }
         }

         Worksheet var15 = this.b.get(var3);
         if (var15.t() != null && var15.getCharts().getCount() > 0) {
            var15.getCharts().b(var1);
         }

         ConditionalFormattingCollection var16 = var15.getConditionalFormattings();

         for(var7 = 0; var7 < var16.getCount(); ++var7) {
            FormatConditionCollection var8 = var16.get(var7);

            for(int var9 = 0; var9 < var8.getCount(); ++var9) {
               FormatCondition var10 = var8.get(var9);
               var10.f();
               byte[] var11;
               if (var10.a() != null) {
                  var11 = var10.a();
                  zyo.a(var11, -1, var11.length - 1, (HashMap)var1, (WorksheetCollection)this.b);
               }

               if (var10.c() != null) {
                  var11 = var10.c();
                  zyo.a(var11, -1, var11.length - 1, (HashMap)var1, (WorksheetCollection)this.b);
               }
            }
         }

         ValidationCollection var17 = var15.getValidations();

         for(int var18 = 0; var18 < var17.getCount(); ++var18) {
            Validation var19 = var17.get(var18);
            byte[] var20;
            if (var19.d() != null) {
               var20 = (byte[])var19.d();
               zyo.a(var20, 0, var20.length - 1, (HashMap)var1, (WorksheetCollection)this.b);
            }

            if (var19.e() != null) {
               var20 = (byte[])var19.e();
               zyo.a(var20, 0, var20.length - 1, (HashMap)var1, (WorksheetCollection)this.b);
            }
         }
      }

      boolean[] var13 = new boolean[this.getCount()];

      for(int var14 = 0; var14 < this.getCount(); ++var14) {
         var13[var14] = (Boolean)var1.get(var14);
      }

      return var13;
   }

   public void clear() {
      boolean[] var1 = this.e();
      HashMap var2 = new HashMap();
      this.c = null;
      int var3 = 0;

      for(int var4 = 0; var4 < this.getCount(); ++var4) {
         Name var5 = this.get(var4);
         if (var1[var3]) {
            var2.put(var3, var2.size());
            var5.u();
         } else {
            this.a.remove(var4--);
         }

         ++var3;
      }

      this.b(var2);
   }

   void b() {
      this.c = null;
      this.a.clear();
   }

   public void removeDuplicateNames() {
      this.c();
      this.c = null;
      HashMap var1 = new HashMap();
      boolean[] var2 = new boolean[this.getCount()];
      int var3 = 0;

      int var4;
      for(var4 = 0; var4 < this.getCount(); ++var4) {
         if (var2[var4]) {
            ++var3;
         } else {
            Name var5 = this.get(var4);
            var1.put(var4, var4 - var3);

            for(int var6 = var4 + 1; var6 < this.getCount(); ++var6) {
               Name var7 = this.get(var6);
               if (var5.getSheetIndex() == var7.getSheetIndex() && com.aspose.cells.b.a.zv.a(var5.i(), var7.i(), true) == 0) {
                  var2[var6] = true;
                  var1.put(var6, var4 - var3);
               }
            }
         }
      }

      for(var4 = 0; var4 < this.getCount(); ++var4) {
         if (var2[var4]) {
            this.a.remove(var4);
         }
      }

      this.b(var1);
   }

   public void sort() {
      this.c();
      this.c = null;
      HashMap var1 = zaog.a(this.b);
      this.b(var1);
   }

   private void b(HashMap var1) {
      if (var1.size() != 0) {
         int var2;
         for(var2 = 0; var2 < this.getCount(); ++var2) {
            byte[] var3 = this.get(var2).b();
            if (var3 != null) {
               zyo.b(var3, -1, var3.length - 1, var1, this.b);
            }
         }

         for(var2 = 0; var2 < this.b.getCount(); ++var2) {
            Worksheet var11 = this.b.get(var2);
            zaau var4 = var11.getCells().b;
            int var7;
            if (var4.c > 0) {
               zyi[] var5 = var4.b;

               for(var7 = 0; var7 < var5.length; ++var7) {
                  if (var5[var7] != null) {
                     zyi var6 = var5[var7];
                     if (var6.a() != null) {
                        zyo.b(var6.a().d(), -1, -1, var1, this.a());
                     } else {
                        zyo.b(var6.b, -1, -1, var1, this.a());
                     }
                  }
               }
            }

            if (var11.t() != null) {
               var11.getShapes().a(var1);
            }

            ConditionalFormattingCollection var12 = this.b.get(var2).getConditionalFormattings();

            for(int var13 = 0; var13 < var12.getCount(); ++var13) {
               FormatConditionCollection var15 = var12.get(var13);

               for(int var8 = 0; var8 < var15.getCount(); ++var8) {
                  FormatCondition var9 = var15.get(var8);
                  var9.f();
                  byte[] var10;
                  if (var9.a() != null) {
                     var10 = (byte[])var9.a();
                     zyo.b(var10, -1, var10.length - 1, var1, this.b);
                  }

                  if (var9.c() != null) {
                     var10 = (byte[])var9.c();
                     zyo.b(var10, -1, var10.length - 1, var1, this.b);
                  }
               }
            }

            ValidationCollection var14 = var11.getValidations();

            for(var7 = 0; var7 < var14.getCount(); ++var7) {
               Validation var16 = var14.get(var7);
               byte[] var17;
               if (var16.d() != null) {
                  var17 = (byte[])var16.d();
                  zyo.b(var17, 0, var17.length - 1, var1, this.b);
               }

               if (var16.e() != null) {
                  var17 = (byte[])var16.e();
                  zyo.b(var17, 0, var17.length - 1, var1, this.b);
               }
            }
         }

      }
   }

   int a(String var1, int var2, boolean var3) {
      var1 = var1.toUpperCase();
      Object var4 = this.d().get(e(var2, var1));
      if (var4 != null) {
         return (Integer)var4;
      } else {
         if (var3 && var2 >= 0) {
            var4 = this.d().get(var1);
            if (var4 != null) {
               return (Integer)var4;
            }
         }

         return -1;
      }
   }

   Name a(String var1, int var2) {
      int var3 = this.a(var1, var2, false);
      return var3 == -1 ? null : this.get(var3);
   }

   int b(String var1, int var2) {
      return this.a(var1, var2, false);
   }

   int[] a(int var1, String var2, boolean var3, boolean var4) {
      int var5 = -1;
      int var6 = var2.indexOf("!");
      int var7 = -1;
      int var8 = -1;
      if (var6 != -1) {
         String var9 = var2.substring(0, 0 + var6);
         var2 = var2.substring(var6 + 1);
         String var10 = var2.toUpperCase();
         int[] var11 = zyn.a(this.b, var9);
         var7 = var11[0];
         int var12 = var11[1];
         var5 = var11[2];
         int var13 = var11[3];
         if (var12 != this.b.u()) {
            zbqg var14 = this.b.v().a(var12);
            if (var14.b().size() > 0) {
               for(int var15 = 0; var15 < var14.b().size(); ++var15) {
                  zuk var16 = (zuk)var14.b().get(var15);
                  if (com.aspose.cells.b.a.zv.b(var16.e().toUpperCase(), var10)) {
                     var8 = var15;
                  }
               }
            }

            if (var8 == -1) {
               zuk var18 = new zuk(var14);
               var18.a(var2);
               var8 = com.aspose.cells.b.a.a.ze.a(var14.b(), (Object)var18);
            }

            return new int[]{var7, var8};
         }

         if (var5 != -1) {
            var8 = this.a(var2, var5, true);
         } else {
            var8 = this.a(var2, var1, true);
         }
      } else {
         var8 = this.a(var2, var1, true);
      }

      Name var17;
      if (var8 == -1) {
         if (var4) {
            var17 = new Name(this.b, var2);
            var17.setSheetIndex(var5 + 1);
            var8 = this.a(var17, true);
            if (var3) {
               var17.a((int)14);
            }
         }
      } else if (var7 != -1) {
         var17 = this.get(var8);
         if (var17.getSheetIndex() == 0) {
            var7 = -1;
         }
      }

      return new int[]{var7, var8};
   }

   void c(int var1, String var2) {
      int var3 = this.a(var2, var1, false);
      if (var3 > -1) {
         Name var4 = this.get(var3);
         if (var4.n() == 0) {
            var4.a(true);
            var4.u();
         }
      }

   }

   void a(int var1) {
      HashMap var2 = new HashMap();

      for(int var3 = 0; var3 < this.a.size(); ++var3) {
         Name var4 = (Name)this.a.get(var3);
         if (var4.getSheetIndex() == var1 + 1) {
            var2.put(var3, false);
         } else if (var4.getSheetIndex() > var1 + 1) {
            var4.r();
         }
      }

      this.a(var2);
   }

   void b(int var1) {
      ++var1;
      this.c = null;

      for(int var2 = 0; var2 < this.a.size(); ++var2) {
         Name var3 = (Name)this.a.get(var2);
         if (var3.getSheetIndex() >= var1) {
            var3.setSheetIndex(var3.getSheetIndex() + 1);
         }
      }

   }

   void a(int var1, int var2) {
      this.c = null;

      for(int var3 = 0; var3 < this.a.size(); ++var3) {
         Name var4 = (Name)this.a.get(var3);
         int var5 = var4.getSheetIndex() - 1;
         if (var5 == var1) {
            var4.setSheetIndex(var2 + 1);
         } else if (var1 > var2) {
            if (var5 < var1 && var5 >= var2) {
               var4.setSheetIndex(var4.getSheetIndex() + 1);
            }
         } else if (var1 < var2 && var5 > var1 && var5 <= var2) {
            var4.setSheetIndex(var4.getSheetIndex() - 1);
         }
      }

   }

   void a(NameCollection var1) {
      this.a.clear();
      this.c = null;

      for(int var2 = 0; var2 < var1.getCount(); ++var2) {
         Name var3 = (Name)var1.a.get(var2);
         Name var4 = new Name(this.b);
         var4.a(var3);
         this.a(var4, false);
         var3 = null;
         var4 = null;
      }

   }

   void a(int var1, int var2, int var3) {
      for(int var4 = 0; var4 < this.getCount(); ++var4) {
         Name var5 = this.get(var4);
         if ((var5.getSheetIndex() == 0 || var5.getSheetIndex() == var1 + 1) && var5.b() != null) {
            var5.b(var1, var2, var3);
         }
      }

   }

   void b(int var1, int var2, int var3) {
      for(int var4 = 0; var4 < this.getCount(); ++var4) {
         Name var5 = this.get(var4);
         if ((var5.getSheetIndex() == 0 || var5.getSheetIndex() == var1 + 1) && var5.b() != null) {
            var5.a(var1, var2, var3);
         }
      }

   }

   void a(Worksheet var1, CellArea var2, int var3, int var4) {
      for(int var5 = 0; var5 < this.getCount(); ++var5) {
         Name var6 = this.get(var5);
         if ((var6.getSheetIndex() == 0 || var6.getSheetIndex() == var1.getIndex() + 1) && var6.b() != null) {
            zyo.a(var2, var3, var4, var1, false, var6.b(), -1, -1, 0, 0, 0, 0);
            var6.h((String)null);
         }
      }

   }

   void c() {
      for(int var1 = 0; var1 < this.b.getCount(); ++var1) {
         Worksheet var2 = this.b.get(var1);

         try {
            if (var2.hasAutofilter()) {
               int var3 = this.a(var1, "_FILTERDATABASE");
               Name var4 = this.get(var3);
               var4.a(true);
               var4.setSheetIndex(var1 + 1);
               var4.f("_FILTERDATABASE");
               var4.a(var1, var2.getAutoFilter().h());
            } else {
               this.c(var1, "_FILTERDATABASE");
            }
         } catch (Exception var5) {
            throw new CellsException(6, "error in set autofilter");
         }
      }

   }

   Range[] d(int var1, String var2) {
      int var3 = this.a(var2, var1, false);
      if (var3 < 0) {
         return null;
      } else {
         Name var4 = this.get(var3);
         return var4.t();
      }
   }

   void a(int var1, String var2, String var3, boolean var4) {
      int var5;
      Name var6;
      if (var3 != null && !"".equals(var3)) {
         var5 = this.a(var1, var2);
         var6 = this.get(var5);
         var6.f(var2);
         var6.a(this.a().x().a(var1, var3, 0, 0, 2, 32, false, true, true));
      } else {
         var5 = this.a(var2, var1, false);
         if (var5 > -1) {
            var6 = this.get(var5);
            var6.a(true);
            var6.u();
         }
      }

   }

   void a(Name var1, String var2) {
      if (this.c != null) {
         String var3 = e(var1.getSheetIndex() - 1, var1.getText());
         String var4 = e(var1.getSheetIndex() - 1, var2);
         if (this.c.get(var4) != null) {
            throw new CellsException(7, "The defined name[" + var2 + "] already exists.");
         }

         Object var5 = this.c.get(var3);
         if (var5 != null) {
            this.c.remove(var3);
            this.c.put(var4, var5);
         }
      }

   }

   private static String e(int var0, String var1) {
      var1 = var1.toUpperCase();
      return var0 < 0 ? var1 : var0 + "!" + var1;
   }

   private String a(String var1, int[] var2) {
      var2[0] = -1;
      int var3 = var1.lastIndexOf(33);
      if (var3 > 0) {
         String var4 = var1.substring(0, 0 + var3);
         if (var4.charAt(0) == '\'') {
            var4 = var4.substring(1, 1 + (var4.length() - 2));
         }

         var4 = var4.toUpperCase();

         for(int var5 = 0; var5 < this.b.getCount(); ++var5) {
            if (this.b.get(var5).getName().toUpperCase().equals(var4)) {
               var2[0] = var5;
               var1 = var1.substring(var3 + 1);
               break;
            }
         }
      }

      return var1;
   }
}
